﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace NakedSanta
{
    class Ground : GameObject
    {
        private const int FR = 0;
        private const int FL = 1;
        private const int BR = 2;
        private const int BL = 3;

        private int size = 1;

        private int floorSide = 90;

        Color color;


        public Ground(GraphicsDevice graphicsDevice, Vector3 position)
        {
            this.position = position;
            this.graphicsDevice = graphicsDevice;

            InitializeVertices();
            InitializeIndices();

            vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.WriteOnly);
            indexBuffer = new IndexBuffer(graphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);

            vertexBuffer.SetData<VertexPositionColorNormal>(vertices);
            indexBuffer.SetData<short>(indices);
        }

        public Ground(GraphicsDevice graphicsDevice, Color color, Vector3 position, int size)
        {
            this.size = size;
            this.color = color;
            this.position = position;
            this.graphicsDevice = graphicsDevice;

            InitializeVertices();
            InitializeIndices();

            vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.WriteOnly);
            indexBuffer = new IndexBuffer(graphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);

            vertexBuffer.SetData<VertexPositionColorNormal>(vertices);
            indexBuffer.SetData<short>(indices);
        }

        private void InitializeVertices()
        {
            vertices = new VertexPositionColorNormal[floorSide*floorSide*4];
            int count = 0;
            for (int i = 0; i < floorSide; i++)
            {
                for (int j = 0; j < floorSide; j++)
                {
                    if ((i + j) % 2 == 0)
                        color = Color.Gold;
                    else
                    {
                        color = Color.DarkGray;
                    }

                    vertices[count] = new VertexPositionColorNormal(new Vector3(i + 0, 0, j + 0), color);
                    vertices[count++].Normal = Vector3.Up;
                    //front left
                    vertices[count] = new VertexPositionColorNormal(new Vector3(i + 0 + size, 0, j + 0), color);
                    vertices[count++].Normal = Vector3.Up;
                    //back right
                    vertices[count] = new VertexPositionColorNormal(new Vector3(i + size + 0, 0, j + 0 - size), color);
                    vertices[count++].Normal = Vector3.Up;
                    //back left
                    vertices[count] = new VertexPositionColorNormal(new Vector3(i + 0, 0, j + 0 - size), color);
                    vertices[count++].Normal = Vector3.Up;  
                }
                
            }
            
            //color = Color.Black;
            //vertices[0] = new VertexPositionColorNormal(new Vector3(0, 0, 0), color);
            //vertices[0].Normal = Vector3.Up;
            ////front left
            //vertices[1] = new VertexPositionColorNormal(new Vector3(0 + size, 0, 0), color);
            //vertices[1].Normal = Vector3.Up;
            ////back right
            //vertices[2] = new VertexPositionColorNormal(new Vector3(0 + size, 0, 0 - size), color);
            //vertices[2].Normal = Vector3.Up;
            ////back left
            //vertices[3] = new VertexPositionColorNormal(new Vector3(0, 0, 0 - size), color);
            //vertices[3].Normal = Vector3.Up;  
            
        }

        private void InitializeIndices()
        {
            indices = new short[floorSide*floorSide*6];
            for (int i = 0, v=0; i < indices.Length; i+=6, v+=4)
            {
                indices[i] = (short) v; // front left;
                indices[i+1] = (short)(v+3); // back left;
                indices[i+2] = (short) (v+2); // back right;

                indices[i+3] = (short) v; // front left;
                indices[i+4] = (short) (v+2); // back left;
                indices[i+5] = (short) (v+1); // back right;  
            }
            
            //indices[0] = 0; // front left;
            //indices[1] = 3; // back left;
            //indices[2] = 2; // back right;

            //indices[3] = 0; // front left;
            //indices[4] = 2; // back left;
            //indices[5] = 1; // back right;
        }

        public void Draw()
        {
            graphicsDevice.SetVertexBuffer(vertexBuffer);
            graphicsDevice.Indices = indexBuffer;
            graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);
        }
    }
}
